package com.swfee.qinben;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.impl.cookie.DateUtils;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.swfee.qinben.base.TabViewflipperBaseActivity;
import com.swfee.qinben.dto.active.ScheduleModel;
import com.swfee.qinben.dto.rsp.ScheduleRspDto;
import com.swfee.qinben.httpactions.ScheduleAction;
import com.swfee.qinben.util.CurenActivitUtils;

public class MyScheduleListActivity extends TabViewflipperBaseActivity {

	private LayoutInflater inflater;

	Handler handle = new Handler();
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//
		// 加载数据
		//

		asynInitData();
	}

	public void asynInitData() {

		showProgressDialog("加载日程数据，请稍候..");
		
		
		new Thread(new Runnable() {

			@Override
			public void run() {
				
				String date = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
				List<ScheduleRspDto> allDtoList = new ScheduleAction()
				.getScheduleListByDay(date);
				List<ScheduleModel> allList = new ArrayList<ScheduleModel>();
				List<ScheduleModel> willList = new ArrayList<ScheduleModel>();
				List<ScheduleModel> okedList = new ArrayList<ScheduleModel>();
				if (allDtoList != null) {
					for (ScheduleRspDto dto : allDtoList) {
						ScheduleModel mod = new ScheduleModel(dto.getScheduleId(),
								dto.getStartTime(), dto.getEndTime(), dto.getTitle());
						mod.setStatus(dto.getStatus());
						allList.add(mod);
						if (dto.getStatus() == 0) {
							willList.add(mod);
						} else {
							okedList.add(mod);
						}
					}
				}
				final ScheduleListAdapter allAdapter = new ScheduleListAdapter(allList);
				final ScheduleListAdapter willAdapter = new ScheduleListAdapter(willList);
				final ScheduleListAdapter okedAdapter = new ScheduleListAdapter(okedList);
				handle.post(new Runnable() {
					
					@Override
					public void run() {
						hideProgressDialog();
						final ListView listViewAll = (ListView) findViewById(R.id.listView_all);
						final ListView listViewOked = (ListView) findViewById(R.id.listView_oked);
						final ListView listViewWillStart = (ListView) findViewById(R.id.listView_wiil_start);
						listViewAll.setAdapter(allAdapter);
						listViewWillStart.setAdapter(willAdapter);
						listViewOked.setAdapter(okedAdapter);
					}
				});

			}

		}).start();

	}

	

	// 适配器
	private final class ScheduleListAdapter extends BaseAdapter {

		private List<ScheduleModel> list;

		ScheduleListAdapter(List<ScheduleModel> list) {
			this.list = list;

		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.schedule_include, null);
				
				ScheduleModel model = list.get(position);
				CurenActivitUtils.setTextViewContent(R.id.start_time, model.getStartTime(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.end_time, model.getEndTime(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.title, model.getTitle(), convertView);
				Button view_opt = (Button) convertView.findViewById(R.id.view_opt);
				if (model.getStatus() != 0) {
					view_opt.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_look));
					view_opt.setTextColor(Color.BLACK);
					view_opt.setText("查看");
				}else{
					view_opt.setOnClickListener(new OperatorClickListener(model));
				}

			}
			return convertView;
		}

	}

	private class OperatorClickListener implements OnClickListener {
		
		private ScheduleModel model = null;
		
		public OperatorClickListener(ScheduleModel model){
			this.model = model;
		}
		
		@Override
		public void onClick(View v) {
			Bundle bu = new Bundle();
			bu.putParcelable("scheduleModel", model);
			CurenActivitUtils.startActivity(ScheduleOperActivity.class, false, bu);

		}

	}

	@Override
	public int getLayoutId() {
		return R.layout.sub_main;
	}

}